import 'package:flame/game.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'package:vector_math/vector_math.dart' as vector;

class ThreeDGamePage extends StatelessWidget {
  const ThreeDGamePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('3D Game'),
        backgroundColor: const Color(0xFF1a237e),
      ),
      body: GameWidget(
        game: ThreeDGame(),
      ),
    );
  }
}

class ThreeDGame extends FlameGame with TapDetector {
  late CameraComponent camera;
  late List<CubeFace> cubeFaces;
  late Vector2 center;
  double rotationAngle = 0;
  double rotationSpeed = 0.02;

  @override
  Future<void> onLoad() async {
    // 设置相机
    camera = CameraComponent();
    camera.viewfinder.zoom = 0.5;
    add(camera);

    // 计算屏幕中心
    center = size / 2;

    // 创建立方体的六个面
    cubeFaces = [
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.red,
        depth: 0,
      ),
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.blue,
        depth: -100,
      ),
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.green,
        depth: 0,
        baseRotation: math.pi / 2,
      ),
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.yellow,
        depth: 0,
        baseRotation: -math.pi / 2,
      ),
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.purple,
        depth: 0,
        baseRotation: math.pi / 2,
        axis: Axis.horizontal,
      ),
      CubeFace(
        position: center,
        size: Vector2.all(100),
        color: Colors.orange,
        depth: 0,
        baseRotation: -math.pi / 2,
        axis: Axis.horizontal,
      ),
    ];

    // 添加所有面到游戏中
    for (var face in cubeFaces) {
      add(face);
    }
  }

  @override
  void update(double dt) {
    super.update(dt);
    rotationAngle += rotationSpeed * dt;

    // 更新所有面的位置和旋转
    for (var face in cubeFaces) {
      face.updateRotation(rotationAngle);
    }
  }

  @override
  bool onTapDown(TapDownInfo info) {
    // 点击屏幕时改变旋转方向
    rotationSpeed = -rotationSpeed;
    return true;
  }
}

class CubeFace extends PositionComponent {
  final Color color;
  final double depth;
  final Axis axis;
  final double baseRotation;
  double currentRotation = 0;

  CubeFace({
    required Vector2 position,
    required Vector2 size,
    required this.color,
    required this.depth,
    this.baseRotation = 0,
    this.axis = Axis.vertical,
  }) : super(position: position, size: size);

  @override
  void render(Canvas canvas) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    // 绘制面
    canvas.drawRect(
      Rect.fromCenter(
        center: Offset.zero,
        width: size.x,
        height: size.y,
      ),
      paint,
    );
  }

  void updateRotation(double angle) {
    currentRotation = baseRotation + angle;
    
    // 根据深度和旋转计算位置
    final offset = Vector2(
      depth * math.cos(currentRotation),
      depth * math.sin(currentRotation),
    );

    // 根据旋转轴调整位置
    if (axis == Axis.horizontal) {
      position = Vector2(
        center.x + offset.x,
        center.y,
      );
    } else {
      position = Vector2(
        center.x,
        center.y + offset.y,
      );
    }
  }
} 